﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ClassLibraries
{
    public class WriteFiles
    {
        #region  返回信息的控制
        /// <summary>
        /// 写错误事件日志
        /// </summary>
        /// <param name="ex">错误事件</param>
        public static void WriteLog(Exception ex)
        {
            //如果日志文件为空，则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
            //if (LogAddress == "")
            //{
            //    LogAddress = Environment.CurrentDirectory + '\\' +
            //        DateTime.Now.Year + '-' +
            //        DateTime.Now.Month + '-' +
            //        DateTime.Now.Day + "_Log.log";
            //}

            //把异常信息输出到文件
            FileStream fs = new FileStream("sys_log.log", FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);

            sw.WriteLine("当前时间：" + DateTime.Now.ToString());
            sw.WriteLine("异常信息：" + ex.Message);
            sw.WriteLine("异常对象：" + ex.Source);
            sw.WriteLine("调用堆栈：\n" + ex.StackTrace.Trim());
            sw.WriteLine("触发方法：" + ex.TargetSite);
            sw.WriteLine();
            sw.Close();
            fs.Close();
        }
        #endregion
        /// <summary>
        /// 写消息
        /// </summary>
        /// <param name="Msg">消息内容</param>
        public static void WriteLog(string Msg)
        {
            FileStream fs = new FileStream("sys_log.log", FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);

            sw.WriteLine("当前时间：" + DateTime.Now.ToString());
            sw.WriteLine("消息：" + Msg);
            sw.WriteLine();
            sw.Close();
            fs.Close();
        }
    }
}
